package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.ConfirmPasswordLoader;
import cn.sirun.com.fca.account.model.login.AuthenticateRequest;
import cn.sirun.com.fca.account.model.login.AuthenticateResponse;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.observer.CommonExceptionObserver;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.IConfirmPasswordPresenter;
import cn.sirun.com.fca.account.service.LoginManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.BehaviorSubject;

public class ConfirmPasswordPresenter extends AccountFlowPresenter<ConfirmPasswordLoader>
        implements IConfirmPasswordPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected ConfirmPasswordLoader createLoader() {
        return new ConfirmPasswordLoader();
    }

    @Override
    public void challenge() {
        getLoader().getChallenge()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        onAction(
                                AccountActionConst.Normal.ACTION_MODIFY_PASS_CHALLENGE_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void authenticate(final AuthenticateRequest request) {
        getLoader().authenticate(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<AuthenticateResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<AuthenticateResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_MODIFY_PASS_AUTHENTICATE_RESPONSE,
                                response);
                    }
                });
    }

    @Override
    public void getSetParams() {
        getLoader().getSetParams()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        onAction(
                                AccountActionConst.Normal.ACTION_MODIFY_PASS_CHALLENGE_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void setPassword(String password) {
        getLoader().setPassword(password)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        getActionListener().onAction(
                                AccountActionConst.Normal.ACTION_MODIFY_LOGIN_PASS_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void changeGetChallenge() {
        getLoader().changeGetChallenge()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        onAction(
                                AccountActionConst.Normal.ACTION_MODIFY_PASS_CHALLENGE_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void changeAuthenticate(AuthenticateRequest request) {
        getLoader().changeAuthenticate(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonExceptionObserver<AuthenticateResponse>(this) {
                    @Override
                    public void onNext(BaseResponse<AuthenticateResponse> response) {
                        super.onNext(response);
                        onAction(AccountActionConst.Normal.ACTION_MODIFY_PASS_AUTHENTICATE_RESPONSE,
                                response);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
